package com.mrx.spring.example.model.converter;

import com.mrx.commons.model.converter.BaseConverter;
import com.mrx.spring.example.model.dto.UserDTO;
import com.mrx.spring.example.model.po.UserPO;
import com.mrx.spring.example.model.vo.UserVO;
import com.mrx.spring.security.model.ApiPermission;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Mr.X
 * @since 2024-02-24 下午 12:01:05
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface UserConverter extends BaseConverter<UserPO, UserDTO, UserVO> {

    default Set<ApiPermission> permissions2ApiPermission(Set<String> permissions) {
        if (CollectionUtils.isEmpty(permissions)) {
            return Collections.emptySet();
        }
        return permissions.stream().map(ApiPermission::of).collect(Collectors.toSet());
    }

    default Set<String> apiPermissions2Permission(Set<ApiPermission> apiPermissions) {
        if (CollectionUtils.isEmpty(apiPermissions)) {
            return Collections.emptySet();
        }
        return apiPermissions.stream().map(ApiPermission::getPath).collect(Collectors.toSet());
    }

}
